﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;

namespace UtfprLibrary.Windows.Forms {
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(ResourceFinder), "Resources.VSD_UtfprCheckBox.png")]
    [Designer("UtfprLibrary.Windows.Forms.ComponentModel.UtfprCheckBoxDesigner, UtfprLibrary.WindowsForms.Design")]
    public class UtfprCheckBox : UtfprCheckBoxBase {
        #region Properties

        private bool _isNumeric = false;
        [Category("Behavior")]
        [Description("")]
        public bool IsNumeric {
            get { return _isNumeric; }
            set {
                _isNumeric = value;
                OnPropertyChanged("IsNumeric");
            }
        }

        private string _checkedValue = "S";
        [Category("Behavior")]
        [Description("")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprCheckBoxEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public string CheckedValue {
            get { return _checkedValue; }
            set {
                _checkedValue = value;
                OnPropertyChanged("CheckedValue");
            }
        }

        private string _uncheckedValue = "N";
        [Category("Behavior")]
        [Description("")]
        [Editor("UtfprLibrary.Windows.Forms.ComponentModel.UtfprCheckBoxEditor, UtfprLibrary.WindowsForms.Design", typeof(UITypeEditor))]
        public string UncheckedValue {
            get { return _uncheckedValue; }
            set {
                _uncheckedValue = value;
                OnPropertyChanged("UncheckedValue");
            }
        }

        [Browsable(false)]
        [Bindable(true)]
        public object ValueField {
            get {
                if (Checked) {
                    return this.CheckedValue;
                } else {
                    return this.UncheckedValue;
                }
            }
            set {
                if (!UtfprControlBase.IsInDesignMode) {
                    if (value.ToString() == this._checkedValue) {
                        this.Checked = true;
                    } else if (CanUncheck(value)) {
                        this.Checked = false;
                    }
                }
            }
        }

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName) {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Methods

        private bool CanUncheck(object value) {
            return
                value.ToString() == this._uncheckedValue ||
                value.ToString() == "" ||
                value == null ||
                value.GetType() == typeof(DBNull);
        }

        #endregion
    }
}
